<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="75px"
    >
      <el-form-item label="代码" prop="supplierNo">
        <el-input
          v-model="queryParams.supplierNo"
          placeholder="请输入供应商代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快捷码" prop="shortcutCode">
        <el-input
          v-model="queryParams.shortcutCode"
          placeholder="请输入快捷码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入供应商地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactor">
        <el-input
          v-model="queryParams.contactor"
          placeholder="请输入供应商联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="移动电话" prop="telephoneNumber">
        <el-input
          v-model="queryParams.telephoneNumber"
          placeholder="请输入供应商移动电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="固定电话" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入固定电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否冻结" prop="frozenFlag">
        <el-select
          v-model="queryParams.frozenFlag"
          placeholder="请选择供应商是否冻结"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in frozenFlagOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有效" prop="deleteFlag">
        <el-select
          v-model="queryParams.deleteFlag"
          placeholder="请选择是否有效"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in deleteFlagOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['systembasic:supplier:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['systembasic:supplier:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['systembasic:supplier:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['systembasic:supplier:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="supplierList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商代码" align="center" prop="supplierNo" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="快捷码" align="center" prop="shortcutCode" />
      <el-table-column label="供应商地址" align="center" prop="address" />
      <el-table-column label="供应商联系人" align="center" prop="contactor" />
      <el-table-column
        label="供应商移动电话"
        align="center"
        prop="telephoneNumber"
      />
      <el-table-column label="固定电话" align="center" prop="phonenumber" />
      <el-table-column
        label="供应商是否冻结"
        align="center"
        prop="frozenFlag"
        :formatter="frozenFlagFormat"
      />
      <el-table-column
        label="是否有效"
        align="center"
        prop="deleteFlag"
        :formatter="deleteFlagFormat"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['systembasic:supplier:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['systembasic:supplier:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="supplierNo">
          <el-input
            v-bind:disabled="isEdit"
            v-model="form.supplierNo"
            placeholder="请输入供应商编号"
          />
        </el-form-item>
        <el-form-item label="名称" prop="supplierName">
          <el-input
            v-model="form.supplierName"
            placeholder="请输入供应商名称"
          />
        </el-form-item>
        <el-form-item label="快捷码" prop="shortcutCode">
          <el-input v-model="form.shortcutCode" placeholder="请输入快捷码" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入供应商地址" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactor">
          <el-input v-model="form.contactor" placeholder="请输入供应商联系人" />
        </el-form-item>
        <el-form-item label="移动电话" prop="telephoneNumber">
          <el-input
            v-model="form.telephoneNumber"
            placeholder="请输入供应商移动电话"
          />
        </el-form-item>
        <el-form-item label="固定电话" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入固定电话" />
        </el-form-item>
        <el-form-item label="是否冻结">
          <el-radio-group v-model="form.frozenFlag">
            <el-radio
              v-for="dict in frozenFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否有效">
          <el-radio-group v-model="form.deleteFlag">
            <el-radio
              v-for="dict in deleteFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSupplier,
  getSupplier,
  delSupplier,
  addSupplier,
  updateSupplier,
  exportSupplier,
} from "@/api/systembasic/supplier";

export default {
  name: "Supplier",
  components: {},
  data() {
    return {
      //是否是修改
      isEdit: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商管理表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 供应商是否冻结字典
      frozenFlagOptions: [],
      // 是否有效字典
      deleteFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierNo: null,
        supplierName: null,
        shortcutCode: null,
        address: null,
        contactor: null,
        telephoneNumber: null,
        phonenumber: null,
        frozenFlag: null,
        deleteFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supplierNo: [
          { required: true, message: "供应商代码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_yes_no").then((response) => {
      this.frozenFlagOptions = response.data;
    });
    this.getDicts("sys_yes_no").then((response) => {
      this.deleteFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询供应商管理列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then((response) => {
        this.supplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 供应商是否冻结字典翻译
    frozenFlagFormat(row, column) {
      return this.selectDictLabel(this.frozenFlagOptions, row.frozenFlag);
    },
    // 是否有效字典翻译
    deleteFlagFormat(row, column) {
      return this.selectDictLabel(this.deleteFlagOptions, row.deleteFlag);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rowId: null,
        supplierNo: null,
        supplierName: null,
        shortcutCode: null,
        address: null,
        contactor: null,
        telephoneNumber: null,
        phonenumber: null,
        frozenFlag: "0",
        deleteFlag: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.supplierNo);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isEdit = false;
      this.open = true;
      this.title = "添加供应商管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isEdit = true;
      const supplierNo = row.supplierNo || this.ids;
      getSupplier(supplierNo).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.rowId != null) {
            updateSupplier(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupplier(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const supplierNos = row.supplierNo || this.ids;
      this.$confirm(
        '是否确认删除供应商管理编号为"' + supplierNos + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delSupplier(supplierNos);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有供应商管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportSupplier(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>